﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAL;

namespace Logic.DataAccess
{
	public class ParameterHelper
	{
		List<SqlParameter> pars;
		public ParameterHelper()
		{ 
			pars = new List<SqlParameter>(); 
		}
		public ParameterHelper(bool withActive)
		{
			pars = new List<SqlParameter>();
			if(withActive)
				pars.Add(GetActiveParam());
		}

		public void Add(string name, object value)
		{ 
			pars.Add(new SqlParameter(name,value));
		}

		public SqlParameter[] GetArray()
		{
			return pars.ToArray();
		}



		public static SqlParameter GetActiveParam()
		{
			return new SqlParameter("active", BLL.EntityStatuses.Active);
		}

		public static List<SqlParameter> GetParamsList(bool withActive, params SqlParameter[] parameters)
		{			
			List<SqlParameter> pars = parameters.ToList();
			if(withActive)
				pars.Add(GetActiveParam());

			return pars;
		}
	}

	public class Stats
	{
		/// <summary>
		/// Retrieves the fan sums of all the account in the master account, grouped by their date 
		/// </summary>
		/// <param name="masterAccountID"></param>
		/// <param name="since"></param>
		/// <returns></returns>
		public static DataTable GetMaterAccountDailyStats(int masterAccountID, DateTime? since)
		{
			var pars = new List<SqlParameter> { new SqlParameter("masterAccountID", masterAccountID) };

			string time = "";
			if (since.HasValue)
			{
				time = "AND dbo.AccountStats.[Date] > @since";
				pars.Add(new SqlParameter("since", since.Value));

			}

			string cmd = string.Format(@"
SELECT 
	AccountStats.Date,
	 SUM(dbo.AccountStats.FbPageFanNum) FbPageFanNum,
	 SUM(dbo.AccountStats.FbFriendsNum) FbFriendsNum,
 SUM(dbo.AccountStats.TwFollowersNum) as TwFollowersNum
FROM dbo.AccountStats
INNER JOIN dbo.Accounts ON dbo.AccountStats.AccountID = dbo.Accounts.ID
WHERE dbo.Accounts.MasterAccountID = @masterAccountID
{0}
GROUP  BY dbo.AccountStats.[Date]
", time);

			return DBHelper.GetTable(cmd, pars.ToArray());
		}

		//public static DataTable GetActiveAccountsWithTraces


		internal static DataTable GetPostsOfMasterAccount(int mAccountID, BO.Post.PostTypes? postType, int count)
		{			
			var pars = new ParameterHelper(true);
			pars.Add("mAccountID", mAccountID);
			pars.Add("count", count);

			var topClause = " TOP " + count.ToString();
			string postTypeClause = "";
			if (postType.HasValue)
			{
				postTypeClause = "AND EnumPostType = @type";
				pars.Add("type", (int)postType.Value);
			}


			string cmd = string.Format(@"
SELECT {0} dbo.Posts.* FROM dbo.Posts
INNER JOIN dbo.Accounts ON dbo.Posts.AccountID = dbo.Accounts.ID
WHERE MasterAccountID = @mAccountID
AND dbo.Posts.StatusEnum = @active
{1}
ORDER BY TimePosted DESC
", topClause,postTypeClause);

			return	DAL.DBHelper.GetTable(cmd, pars.GetArray());
		}

		internal static DataTable GetPostsOfProfile(int profileID, BO.Post.PostTypes? postType, int count)
		{
			var pars = new ParameterHelper(true);
			pars.Add("profileID", profileID);
			pars.Add("count", count);

			var topClause = " TOP " + count.ToString();
			string postTypeClause = "";
			if (postType.HasValue)
			{
				postTypeClause = "AND EnumPostType = @type";
				pars.Add("type", (int)postType.Value);
			}

			string cmd = string.Format(@"
SELECT {0} dbo.Posts.* FROM dbo.Posts
INNER JOIN dbo.Accounts ON dbo.Posts.AccountID = dbo.Accounts.ID
INNER JOIN dbo.MasterAccounts ON dbo.Accounts.MasterAccountID = dbo.MasterAccounts.ID
WHERE MasterAccounts.ProfileID = @profileID
AND dbo.Posts.StatusEnum = @active
{1}
ORDER BY TimePosted DESC
", topClause, postTypeClause);

			return DAL.DBHelper.GetTable(cmd, pars.GetArray());
		}
	}
}
